package com.fusion.core.comm.rpc;

import com.fusion.core.comm.Packet;
import com.fusion.core.comm.RPC;
import com.fusion.core.comm.T;
import com.fusion.core.file.filedatabase.FileDescriptor;

import java.io.IOException;
import java.util.Collection;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 8, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class GetHashesForPath extends RPC {
    private String path;
    private int shareBaseIndex;

    public GetHashesForPath() {
    }

    public GetHashesForPath(int shareBaseIndex, String path) {
        this.shareBaseIndex = shareBaseIndex;
        this.path = path;
    }

    public void execute(Packet data) throws IOException {
        shareBaseIndex = data.readInt();
        path = data.readUTF();

        String basePath = core.getShareManager().getBaseByIndex(shareBaseIndex).getPath();
        path = basePath += '/'+path;

        Collection<FileDescriptor> c = core.getFileManager().getFileDatabase().getFDsByPath(path);
        if(T.t)T.info("Found "+c.size()+" hashes for path "+path);
        send(new HashesForPath(path, c));
    }

    public Packet serializeTo(Packet p) {
        p.writeInt(shareBaseIndex);
        p.writeUTF(path);
        return p;
    }
}

